﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


public class ComposeItemDrag : MonoBehaviour, IPointerDownHandler, IDragHandler, IPointerUpHandler
{
    [AddComponentMenu("ComposeItemDrag")]
    private RectTransform mRecTrans;
    private Vector3 mBeginPos;
    private Vector3 mOffPos;
    private string mCurSlot;
    private GameObject mDragTop;
    private Transform mSlotObj;

    private LuaInterface.LuaTable mEventSysTab;
    private LuaInterface.LuaFunction mFireEventFunc;

    RectTransform rectTransform
    {
        get
        {   
            if(mRecTrans == null)
                mRecTrans = transform.GetComponent<RectTransform>();

            return mRecTrans;
        }
    }

    public void Awake()
    {
        mEventSysTab = SDK.Scripts.App.GameSDK.Lua.GetLuaTable("EventSys");
        mFireEventFunc = mEventSysTab.GetLuaFunction("FireEvent");

        // 为了使拖动image显示在其他UI之上
        mDragTop = GameObject.Find("DragTop");
    }
       
    public void OnPointerDown(PointerEventData eventData)
    {
        mBeginPos = transform.position;

        //不对齐锚点
        //if(RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out mBeginPos))
        //mOffPos = transform.position - mBeginPos;

        //对齐锚点至鼠标位置
        mOffPos = Vector3.zero;
        mCurSlot = transform.parent.name;

        mSlotObj = transform.parent;
        transform.SetParent(mDragTop.transform, true);
        //通知UI开始拖动
        mFireEventFunc.Call(mEventSysTab, "Compose_Start_Drag", mCurSlot);
    }

    public void OnDrag(PointerEventData eventData)
    {
        Vector3 currentMousePos;
        if (RectTransformUtility.ScreenPointToWorldPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out currentMousePos))
        {
            //设置位置及偏移量
            transform.position = currentMousePos + mOffPos;
        }
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        GameObject dragSlot = IsDragToSlot();
        if (dragSlot != null)
        {
            transform.SetParent(dragSlot.transform);
            transform.localPosition = Vector3.zero;
            string slot = dragSlot.name;
            if (!slot.Equals(mCurSlot))
            {
                mCurSlot = slot;
            }
        }
        else
        {
            transform.SetParent(mSlotObj, true);
            transform.position = mBeginPos;
        }

        //通知UI拖动到了哪个格子
        mFireEventFunc.Call(mEventSysTab, "Compose_End_Drag", mCurSlot);
    }

    //判断是否拖到格子上
    public GameObject IsDragToSlot()
    {
        GameObject canvas = GameObject.Find("Canvas2D");

        PointerEventData pointerEventData = new PointerEventData(EventSystem.current);
        pointerEventData.position = Input.mousePosition;
        GraphicRaycaster gr = canvas.GetComponent<GraphicRaycaster>();
        List<RaycastResult> results = new List<RaycastResult>();
        gr.Raycast(pointerEventData, results);

        foreach(RaycastResult result in results)
        {
            if (result.gameObject.CompareTag("ComposeSlot"))
            {
                return result.gameObject;
            }else if (result.gameObject.CompareTag("ComposeRecycle"))
            {
                OnRecycle();
                return null;
            }
        }

        return null;
    }

    public void OnRecycle()
    {
        //通知UI回收
        mFireEventFunc.Call(mEventSysTab, "Compose_Drag_Recycle", mCurSlot);
    }


}
